package com.medusa.aps.business.modules.basic.controller;


import com.medusa.aps.business.model.pagination.Page;
import com.medusa.aps.business.modules.basic.model.input.MaterialDTO;
import com.medusa.aps.business.modules.basic.model.input.MaterialListDTO;
import com.medusa.aps.business.modules.basic.model.input.MaterialParam;
import com.medusa.aps.business.modules.basic.model.output.MaterialVO;
import com.medusa.aps.business.modules.basic.mp.entity.Material;
import com.medusa.aps.business.modules.basic.mp.service.MaterialService;
import com.medusa.aps.business.common.model.resp.Result;
import com.medusa.aps.business.common.web.valid.group.Update;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Size;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 物料控制层
 *
 * @author yuxh
 * @since 2023-08-14 17:37:47
 */
@Validated
@RestController
@RequestMapping("/material")
@Tag(name = "物料管理", description = "物料管理")
public class MaterialController {
    /**
     * 服务对象
     */
    @Resource
    private MaterialService materialService;

    /**
     * 分页查询所有数据
     *
     * @param materialParam 分页对象
     * @return 所有数据
     */
    @GetMapping("/selectAll")
    @Operation(summary = "分页查询所有数据")
    public Result<Page<Material>> selectAll(MaterialParam materialParam) {
        return Result.ok(materialService.selectAll(materialParam));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectOne")
    @Operation(summary = "通过主键查询单条数据")
    public Result<Material> selectOne(@RequestParam Integer id) {
        return Result.ok(materialService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param materials 实体对象
     * @return 新增结果
     */
    @PostMapping("/save")
    @Operation(summary = "新增数据")
    public Result<Boolean> saveBatchMaterial(@RequestBody @Valid @Size(min = 1, max = 50, message = "物料个数最小为1,最大为50") List<MaterialDTO> materials) {
        materialService.saveBatchMaterial(materials);
        return Result.ok();
    }

    /**
     * 修改物料
     *
     * @param material 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    @Operation(summary = "修改物料")
    public Result<Boolean> updateMaterial(@RequestBody @Validated(Update.class) MaterialDTO material) {
        materialService.updateMaterial(material);
        return Result.ok();
    }

    /**
     * 删除物料
     *
     * @param materialIds 物料id集合
     */
    @DeleteMapping("/delete")
    @Operation(summary = "删除物料")
    public Result<Void> deleteMaterials(@RequestBody Set<Long> materialIds) {
        materialService.deleteMaterials(materialIds);
        return Result.ok();
    }

    /**
     * 根据物料编码或物料名称查询物料
     *
     * @param materialList 查询参数
     * @return 物料基本信息
     */
    @GetMapping("/getSupplierMaterials")
    @Operation(summary = "根据物料编码或物料名称查询物料")
    public Result<List<MaterialVO>> getSupplierMaterials(MaterialListDTO materialList) {
        return Result.ok(materialService.getSupplierMaterials(materialList));
    }
}

